package com.senthink.gl.business.api.controller;

import com.senthink.gl.business.auth.AccountService;
import com.senthink.gl.business.auth.UpdateUserRequest;
import com.senthink.gl.business.common.entity.PageDto;
import com.senthink.gl.business.common.entity.SearchRequest;
import com.senthink.gl.business.response.ApiResponse;
import com.senthink.gl.business.security.JwtTokenUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * Created by lenovo on 2017/11/23.
 */
@Api(tags = "用户信息controller")
@RestController
public class AccountController {

    @Autowired
    private JwtTokenUtils jwtTokenUtils;
    @Autowired
    private AccountService accountService;

    /**
     * 更新用户信息
     *
     * @param updateUserRequest
     * @return
     */
    @ApiOperation(notes = "更新用户信息",value = "更新用户信息")
    @RequestMapping(value = "${uri.user.update}", method = RequestMethod.POST)
    public ResponseEntity<?> update(@RequestBody @Valid UpdateUserRequest updateUserRequest, @RequestHeader @Value("${jwt.header}")String authorization) {
        ApiResponse apiResponse = accountService.updateUserRequest(updateUserRequest, authorization);
        return ResponseEntity.ok(apiResponse);
    }

    /**
     * 获取用户分页列表
     *
     * @param role
     * @param request
     * @param pageable
     * @return
     */
    @ApiOperation(notes = "获取用户分页列表",value = "获取用户分页列表")
    @RequestMapping(value = "${uri.user.list}", method = RequestMethod.GET)
    public ResponseEntity<?> list(String aid, Boolean enable, String role, @Valid SearchRequest request, @Valid PageDto pageable) {
        ApiResponse apiResponse = accountService.userList(aid, enable, role, request, pageable);
        return ResponseEntity.ok(apiResponse);
    }

    /**
     * 获取某用户详情
     *
     * @param uid
     * @return
     */
    @ApiOperation(notes = "获取某用户详情",value = "获取某用户详情")
    @RequestMapping(value = "${uri.user.detail}", method = RequestMethod.GET)
    public ResponseEntity<?> detail(@RequestParam @Valid String uid) {
        ApiResponse apiResponse = accountService.getDetailByUid(uid);
        return ResponseEntity.ok(apiResponse);
    }

    /**
     * 删除用户
     *
     * @param request
     * @return
     */
    @ApiOperation(notes = "删除用户",value = "删除用户")
    @RequestMapping(value = "${uri.user.delete}", method = RequestMethod.DELETE)
    public ResponseEntity<?> delete(@Validated(UpdateUserRequest.DeleteOneGroup.class) UpdateUserRequest request) {
        ApiResponse apiResponse = accountService.delete(request);
        return ResponseEntity.ok(apiResponse);
    }

    /**
     * 获取登录用户详情
     *
     * @param authorization
     * @return
     */
    @ApiOperation(notes = "获取登录用户详情",value = "获取登录用户详情")
    @RequestMapping(value = "${uri.user.center}", method = RequestMethod.GET)
    public ResponseEntity<?> center(@RequestHeader("${jwt.header}") String authorization) {
        String account = jwtTokenUtils.getUsernameFromAuthorizationHeader(authorization);
        ApiResponse apiResponse = accountService.getDetailByAccount(account);
        return ResponseEntity.ok(apiResponse);
    }

    /**
     * 修改用户禁用属性
     * @param request
     * @return
     */
    @ApiOperation(notes = "修改用户禁用属性",value = "修改用户禁用属性")
    @RequestMapping(value = "${uri.user.enable}", method = RequestMethod.PUT)
    public ResponseEntity<?> enable(@RequestBody @Validated(UpdateUserRequest.EnableOneGroup.class) UpdateUserRequest request) {
        return ResponseEntity.ok(accountService.enabled(request.getUid(), request.isEnable()));
    }
}
